package bao;

import DBCon.DBCon;
import model.User;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;
    PreparedStatement ps = null;
    DBCon dbCon = new DBCon();

//    登录校验方法
    public boolean login(User user) {
        String sql = "SELECT * FROM user WHERE user_name = ? AND user_password = ?";
        conn = dbCon.dbCon();
        if (conn != null) {
            System.out.println("数据库连接成功！");
            try {
                ps = conn.prepareStatement(sql);
                ps.setString(1,user.getUser_name());
                ps.setString(2,user.getUser_password());
                resultSet = ps.executeQuery();
                if(resultSet.next()){
                    user.setReal_name(resultSet.getString("real_name"));
                    System.out.println("登陆成功！");
                    return true;
                }else {
                    System.out.println("登录失败！");
                   return false;
                }
            } catch (Exception e) {
                System.out.println("数据库连接异常！");
                e.printStackTrace();
            }
        }
        dbCon.close();
        return false;
    }

//    注册调用的方法
    public String register(User user) {
        String sql = "insert into user(user_name, user_password, Email, real_name, tel, sex, date_birth) values(?, ?, ?, ?, ?, ?, ?)";
        conn = dbCon.dbCon();
        try {
            ps = conn.prepareStatement(sql);
            System.out.println("用户名："+user.getUser_name());
            ps.setString(1, user.getUser_name());
            ps.setString(2, user.getUser_password());
            ps.setString(3, user.getEmail());
            ps.setString(4, user.getReal_name());
            ps.setString(5, user.getTel());
            ps.setString(6, user.getSex());
            ps.setString(7, user.getDate_birth());
            if (ps.executeUpdate() > 0) {
                System.out.println("注册成功");
                dbCon.close();
                return "注册成功！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "注册失败！";
    }

//    查看用户信息
    public List<User> queryUserList(String name){
        User user;
        List<User> userList = new ArrayList<User>();
        String sql = "select * from user";
        String re_sql = "select * from user where user_name=?";
        conn = dbCon.dbCon();
        try{
            if (name==null){
                statement = conn.createStatement();
                resultSet = statement.executeQuery(sql);
            }else {
                ps = conn.prepareStatement(re_sql);
                ps.setString(1,name);
                resultSet = ps.executeQuery();
            }
            while (resultSet.next()){
                user = new User();
                user.setUser_name(resultSet.getString("user_name"));
                user.setUser_password(resultSet.getString("user_password"));
                user.setEmail(resultSet.getString("Email"));
                user.setSex(resultSet.getString("sex"));
                user.setTel(resultSet.getString("tel"));
                user.setReal_name(resultSet.getString("real_name"));
                user.setDate_birth(resultSet.getString("date_birth"));
                userList.add(user);
            }
            return userList;
        }catch (Exception e){
            System.out.println("数据库连接错误！");
            e.printStackTrace();
        }
        return null;
    }

//    删除用户
    public boolean delete(String name){
        String sql = "delete from user where user_name=?";
        conn = dbCon.dbCon();
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            if ( ps.executeUpdate()>0){
                System.out.println("删除成功！");
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

//    修改用户信息
    public boolean modifyUserInfo(User user){
        conn = dbCon.dbCon();
        String sql = "update user set user_password=?,Email=?,real_name=?,tel=?,data_birth=? where user_name=?";
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUser_password());
            ps.setString(2,user.getEmail());
            ps.setString(1,user.getReal_name());
            ps.setString(1,user.getTel());
            ps.setString(1,user.getSex());
            ps.setString(1,user.getDate_birth());
            ps.setString(1,user.getUser_name());
            if(ps.executeUpdate()>0){
                System.out.println("修改用户成功");
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dbCon.close();
        }
        return false;
    }
}
